﻿using System.Globalization;
using HotelRegistrationSystem.Code;
using System.Threading;
using System.Web;
using System.Web.Http;
using System.Web.Mvc;
using System.Web.Optimization;
using System.Web.Routing;

namespace HotelRegistrationSystem
{
    public class MvcApplication : HttpApplication
    {
        private static Thread _cancelReservationsDeamonThread;

        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();

            WebApiConfig.Register(GlobalConfiguration.Configuration);
            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
            RouteConfig.RegisterRoutes(RouteTable.Routes);
            BundleConfig.RegisterBundles(BundleTable.Bundles);

            GlobalConfiguration.Configuration.Filters.Add(new System.Web.Http.AuthorizeAttribute());
			
            // Start deamon that cancel reservations
            var _deamon = new CancelReservationDeamon();

            // TODO: uncomment when final publishing and test
			_cancelReservationsDeamonThread = new Thread(new ThreadStart(_deamon.MonitorReservations));
			_cancelReservationsDeamonThread.Start();
        }

        protected void Application_End()
        {
            _cancelReservationsDeamonThread.Abort();
        }
    }
}